package cn.kgc.rui;

import java.util.Arrays;

public class GuiBing {
    public static void main(String[] args) {
        int a[] ={3, 1, 4, 5, 2, 6,7};
        int low=0;
        int high=a.length-1;
        Mergesort(a,low,high);
        for (int i : a) {
            System.out.print(i+" ");
        }
    }

    private static void Mergesort(int[] a, int low, int high) {
        if(low<high) {
            int mid=(low+high)/2;
            Mergesort(a,low,mid);
            Mergesort(a,mid+1,high);
            Merge(a,low,mid,high);
            System.out.println(Arrays.toString(a));
        }
    }

    public static void Merge(int[] a, int low, int mid, int high) {
        //定义第一个段的开始
        int s1 = low;
        //定义第二个段的开始
        int s2 = mid+1;
        //定义临时数组
        int[] temp = new int[high-low+1];
        int i = 0;//定义临时数组的下标
        //判断大小将数组放入到临时数组当中去
        while(s1<=mid && s2<=high) {
            if(a[s1] <= a[s2]) {
                temp[i++] = a[s1++];
            }else {
                temp[i++] = a[s2++];
            }
        }
        //判断s1当中是否有数据，如果有将其全部拷贝到临死数组当中去
        while (s1 <= mid) {
            temp[i++] = a[s1++];

        }

        //判断s1当中是否有数据，如果有将其全部拷贝到临死数组当中去
        while (s2 <= high) {
            temp[i++] = a[s2++];

        }

        //将临时数组当中的代码放回原数组
        for (int j = 0; j < temp.length; j++) {
            a[j+low] = temp[j];
        }

    }
}